package com.ssm.rfidreader;


import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ssm.bean.Device;
import com.ssm.service.DeviceService;


@Component
public class ReaderManage {
	
	@Autowired
	private DeviceService deviceService;
	
	//保存所有数据读取线程
    public static ConcurrentMap<String, RfidReader> map = new ConcurrentHashMap<>();
	
	@PostConstruct
	public void openRfid() {
		
	  List<Device> devices = deviceService.getAllDevices(); 
	  for (Device device : devices) { 
		  startDevice(device);
	   }
	}
	
	public void startDevice(Device device){
		if(device.getIpAddress()!=null && device.getPort()!=0 && device.getType()!= null) {
			RfidReader rfidReader = new RfidReader(device.getIpAddress(), device.getPort(), Integer.parseInt(device.getType()));
			map.put(device.getIpAddress(), rfidReader);
			rfidReader.start();
		}
	}
	
	public static void stopRfidByIp(String ip) {
		if(ip != null && map.get(ip) != null) {
			map.get(ip).stop();
		}
	}
}
